package com.neusoft.yiyang.care.controller;

import com.neusoft.yiyang.common.entity.care.CustomerCareItem;
import com.neusoft.yiyang.care.service.CustomerCareItemService;
import com.neusoft.yiyang.common.enums.ExecutionStatus;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "客户护理项目管理接口", description = "管理客户已购买的护理项目及关联操作")
@RestController
@RequestMapping("/customer-care-items")
public class CustomerCareItemController {

    @Autowired
    private CustomerCareItemService customerCareItemService;

    @Operation(summary = "查询客户已购护理项目", description = "获取指定客户当前有效的护理项目列表")
    @GetMapping("/by-customer/{customerId}")
    public List<CustomerCareItem> getCustomerCareItemsByCustomerId(
            @Parameter(description = "客户ID", required = true) @PathVariable Integer customerId) {
        return customerCareItemService.getValidCareItemsByCustomerId(customerId);
    }

    @Operation(summary = "添加客户护理项目", description = "为客户手动添加已购买的护理项目（如单独购买的服务）")
    @PostMapping("/add")
    public void addCustomerCareItem(
            @Parameter(description = "客户护理项目信息（含客户ID、项目ID等）", required = true)
            @RequestBody CustomerCareItem customerCareItem) {
        customerCareItemService.addCustomerCareItem(customerCareItem);
    }

    @Operation(summary = "更新客户护理项目", description = "修改客户已购护理项目的信息（如剩余次数、有效期等）")
    @PutMapping("/{customerCareItemId}")
    public void updateCustomerCareItem(
            @Parameter(description = "客户护理项目ID", required = true) @PathVariable Integer customerCareItemId,
            @Parameter(description = "更新后的客户护理项目信息", required = true)
            @RequestBody CustomerCareItem customerCareItem) {
        customerCareItem.setCustomerCareItemId(customerCareItemId);
        customerCareItemService.updateCustomerCareItem(customerCareItem);
    }

    @Operation(summary = "删除客户护理项目", description = "移除客户已购买的护理项目（谨慎操作，可能影响历史记录）")
    @DeleteMapping("/{customerCareItemId}")
    public void deleteCustomerCareItem(
            @Parameter(description = "客户护理项目ID", required = true) @PathVariable Integer customerCareItemId) {
        customerCareItemService.deleteCustomerCareItem(customerCareItemId);
    }

    @Operation(summary = "按护理等级为客户分配项目", description = "根据护理等级自动为客户添加该等级包含的所有护理项目")
    @PostMapping("/customers/{customerId}/care-level/{careLevelId}")
    public void addCareItemsByCareLevel(
            @Parameter(description = "客户ID", required = true) @PathVariable("customerId") Integer customerId,
            @Parameter(description = "护理等级ID", required = true) @PathVariable("careLevelId") Integer careLevelId) {
        customerCareItemService.addCareItemsByCareLevel(customerId, careLevelId);
    }

    @Operation(summary = "完成护理并更新使用次数", description = "提交护理记录的同时，自动扣减客户该项目的剩余次数")
    @PostMapping("/complete-with-usage")
    public void completeCareRecordWithUsageUpdate(
            @Parameter(description = "客户ID", required = true) @RequestParam("customerId") Integer customerId,
            @Parameter(description = "护理项目ID", required = true) @RequestParam("careItemId") Integer careItemId,
            @Parameter(description = "护工ID", required = true) @RequestParam("caretakerId") Integer caretakerId,
            @Parameter(description = "执行次数（扣减对应的项目剩余次数）", required = true) @RequestParam("quantity") Integer quantity,
            @Parameter(description = "护理反馈内容", required = true) @RequestParam("feedback") String feedback,
            @Parameter(description = "执行状态（normal=已完成/abnormal=未完成/missed=漏执行）", required = true)
            @RequestParam("status") ExecutionStatus status,
            @Parameter(description = "护理证明照片（可选，支持PNG/JPG格式）")
            @RequestParam(value = "file", required = false) MultipartFile file
    ) {
        customerCareItemService.completeCareRecordWithUsageUpdate(
                customerId, careItemId, caretakerId, quantity, feedback, status, file
        );
    }
}